package net.zzy.pbbot.plugin.callMeStudy.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.lz1998.pbbot.utils.Msg;
import net.zzy.pbbot.plugin.callMeStudy.entity.CallMeDaoEntity;
import net.zzy.pbbot.plugin.callMeStudy.mapper.CallMeMapper;
import net.zzy.pbbot.plugin.todo.entity.QqDaoEntity;
import net.zzy.pbbot.plugin.todo.mapper.QqMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhuZhaoYang
 * @date 2021/9/29 20:39
 */
@Component
public class CallMeService {
    private CallMeMapper callMeMapper;

    @Autowired
    public void setCallMeMapper(CallMeMapper callMeMapper) {
        this.callMeMapper = callMeMapper;
    }

    private QqMapper qqMapper;

    @Autowired
    public void setQqMapper(QqMapper qqMapper) {
        this.qqMapper = qqMapper;
    }

    public Msg addCall(String qq) {
        QqDaoEntity number = qqMapper.selectOne(new QueryWrapper<QqDaoEntity>().eq("number", qq));
        CallMeDaoEntity callMeDaoEntity = null;
        if (number == null) {
            number = new QqDaoEntity();
            number.setNumber(Long.valueOf(qq));
            qqMapper.insert(number);
        } else {
            callMeDaoEntity = callMeMapper.selectOne(new QueryWrapper<CallMeDaoEntity>().eq("qq_id", number.getId()));
        }
        if (callMeDaoEntity != null) {
            return Msg.builder().text("你已经设置提醒了。");
        }
        callMeDaoEntity = new CallMeDaoEntity();
        callMeDaoEntity.setQqId(number.getId());
        callMeMapper.insert(callMeDaoEntity);
        return Msg.builder().text("设置提醒成功！");
    }

    public Msg cancelCall(String qq) {
        QqDaoEntity number = qqMapper.selectOne(new QueryWrapper<QqDaoEntity>().eq("number", qq));
        CallMeDaoEntity callMeDaoEntity = callMeMapper.selectOne(new QueryWrapper<CallMeDaoEntity>().eq("qq_id", number.getId()));
        if (callMeDaoEntity == null) {
            return Msg.builder().text("你还没有设置提醒。");
        }
        callMeMapper.delete(new QueryWrapper<CallMeDaoEntity>().eq("qq_id", number.getId()));
        return Msg.builder().text("取消提醒学习成功！");
    }
}
